<template>
  <div>
    <!--搜索表单-->
    <div style="margin: 10px 0">
      <el-input prefix-icon="el-icon-user" style="width: 240px;" placeholder="请输入分类名称"
                v-model="params.name"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search"></i>搜索</el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh"></i>重置</el-button>
    </div>

    <!--数据表格-->
    <el-table :data="tableDate" stripe row-key="id" default-expand-all>
      <el-table-column prop="id" label="编号" width="200px"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间"></el-table-column>


      <el-table-column label="操作" width="260px">
        <template v-slot="scope">

          <!--scope.row就是当前行的数据-->
          <el-button type="success" v-if="!scope.row.pid" @click="handleAdd(scope.row)">添加二级分类</el-button>

          <el-button style="margin-right: 10px" type="primary" @click="$router.push('/editCategory?id='+scope.row.id)">
            编辑
          </el-button>


          <template>
            <el-popconfirm title="您确定删除这行数据吗？" @confirm="del(scope.row.id)">
              <el-button slot="reference" type="danger">删除</el-button>
            </el-popconfirm>
          </template>

        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div style="margin-top: 10px">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>

    <!--添加二级分类的表单-->
    <el-dialog title="添加二级分类" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="80px" ref="ruleForm" :rules="rules" style="width: 85%;margin: 0 auto">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveKindName">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  name: 'Category',
  data() {
    return {
      tableDate: [],       //1.存放数据
      total: 0,           //总条数
      dialogFormVisible: false,
      form: {},
      pid: null,
      params: {            //分页模糊查询参数
        pageSize: 10,
        pageNum: 1,
        name: '',
      },
      rules: {
        name: [
          {required: true, message: '分类名称不能为空', trigger: 'blur'},
        ],
      },
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {}
    }
  },
  created() {					//3.前端页面加载数据
    this.load()
  },
  methods: {
    load() {                 //2.请求后台数据
      request.get('/category/page', {
        params: this.params
      }).then(res => {
        console.log(res.data)
        if (res.code === '200') {
          this.tableDate = res.data.list
          this.total = res.data.total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    reset() {               //重置搜索框数据
      this.params = {
        pageSize: 10,
        pageNum: 1,
        name: '',
      }
      this.load()
    },
    handleCurrentChange(pageNum) {      //点击分页按钮触发分页
      this.params.pageNum = pageNum
      this.load()
    },
    del(id) {     //删除管理员信息
      request.delete("/category/delete/" + id).then(res => {
        if (res.code === '200') {
          this.$notify.success("删除成功")
          this.load()
        } else {
          this.$notify.error("删除失败" + res.msg)
        }
      })
    },
    handleAdd(row) {
      this.pid = row.id         //将当前行的id作为二级分类的pid
      this.dialogFormVisible = true
      this.form = {}
    },
    saveKindName() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.form.pid = this.pid   //给二级分类复制pid
          request.post("/category/save", this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success("新增分类成功")
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$notify.error("新增分类失败")
            }
          })
        }
      })
    }
  }
}
</script>